package com.yzjs.localhost.service.sysUser.impl;

import com.yzjs.common.base.service.BaseServiceImpl;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.localhost.dto.ComparisonResultDTO;
import com.yzjs.localhost.dto.DataBundlingDTO;
import com.yzjs.localhost.dto.UserRightsDTO;
import com.yzjs.localhost.entity.sysUser.SysUserEntity;
import com.yzjs.localhost.dao.sysUser.SysUserMapper;
import com.yzjs.localhost.entity.tDataPersistenceAfter.TDataPersistenceAfterEntity;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.entity.tNeusoftSysRole.TNeusoftSysRoleEntity;
import com.yzjs.localhost.entity.tNeusoftSysRoleMenu.TNeusoftSysRoleMenuEntity;
import com.yzjs.localhost.entity.tNeusoftSysUser.TNeusoftSysUserEntity;
import com.yzjs.localhost.entity.tNeusoftSysUserRole.TNeusoftSysUserRoleEntity;
import com.yzjs.localhost.service.AnalysisService;
import com.yzjs.localhost.service.sysUser.SysUserService;
import com.yzjs.localhost.service.tNeusoftSysUser.TNeusoftSysUserService;
import com.yzjs.localhost.service.tNeusoftSysUserRole.TNeusoftSysUserRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("sysUserService")
public class SysUserServiceImpl extends BaseServiceImpl<SysUserEntity, SysUserMapper> implements SysUserService {

    @Autowired
    private TNeusoftSysUserService neusoftSysUserService ;

    @Autowired
    private TNeusoftSysUserRoleService  neusoftSysUserRoleService ;

    /**
     * 用户检测功能
     *
     * @param dto
     * @param rights
     * @throws Exception
     */
    public ComparisonResultDTO UserCompliance(DataBundlingDTO dto, UserRightsDTO rights) throws Exception {
        TDataPersistenceBeforceEntity beforce = dto.getBeforce();
        String beforceInfo = beforce.getInfo();
        Map<String, Object> beforceParse = JsonUtil.json2map(beforceInfo);
        TDataPersistenceAfterEntity after = dto.getAfter();
        String afterInfo = after.getInfo();

        ComparisonResultDTO resultDTO = null;
        if (beforce.getOperatorCode().contains("user:add")) {
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对 用户名，昵称，性别，部门id，名称
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(), beforceParse, afterParse, "password,nickName,sex,deptId,userName");
        } else if (beforce.getOperatorCode().contains("user:edit")) {
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对 用户名，昵称，性别，部门id，名称
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(), beforceParse, afterParse, "password,nickName,sex,deptId,userName");
        } else if (beforce.getOperatorCode().contains("user:remove")) {
            beforceParse.put("delFlag", "2");
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对 用户状态
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(), beforceParse, afterParse, "userIds,delFlag");
            SysUserEntity userEntity = new SysUserEntity();
            userEntity.setUserId(Long.valueOf((String) afterParse.get("userIds")));
            userEntity.setDelFlag((String) afterParse.get("delFlag"));
            userEntity.setDataIdentification(after.getDataIdentification());
            this.insert(userEntity);
            return resultDTO;
        } else if (beforce.getOperatorCode().contains("user:resetPwd")) {
            // 重置密码比对密码信息即可
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对操作人密码，用户信息
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(), beforceParse, afterParse, "password,userId");
        }

        // 记录表表信息
        SysUserEntity userEntity = JsonUtil.json2pojo(after.getInfo(), SysUserEntity.class);
        userEntity.setPermissionMirroring(JsonUtil.obj2jsonIgnoreNull(rights));
        userEntity.setDataIdentification(after.getDataIdentification());
        this.insert(userEntity);
        SynchronizationSysUser(userEntity,beforce.getOperatorCode());

        return resultDTO;
    }

    @Override
    public void SynchronizationSysUser(SysUserEntity userEntity, String operatorCode) {
        TNeusoftSysUserEntity neusoftSysUser  = new TNeusoftSysUserEntity();
        neusoftSysUser.setUserId(userEntity.getUserId());
        TNeusoftSysUserEntity resultUser = neusoftSysUserService.findOne(neusoftSysUser);

        if(resultUser == null){
            BeanUtils.copyProperties(userEntity,neusoftSysUser);
            neusoftSysUserService.insert(neusoftSysUser);
        } else {
            BeanUtils.copyProperties(userEntity,resultUser);
            neusoftSysUserService.update(resultUser);
        }
        // 更新角色菜单信息
        TNeusoftSysUserRoleEntity neusoftSysRole = new TNeusoftSysUserRoleEntity();
        neusoftSysRole.setUserId(userEntity.getUserId());

        List<TNeusoftSysUserRoleEntity> userRoles = neusoftSysUserRoleService.findList(neusoftSysRole);
        if(userRoles != null){
            userRoles.forEach(x->{
                neusoftSysUserRoleService.delete(x);
            });
        }

        List<Integer> roleIds= userEntity.getRoleIds();
        if (roleIds != null){
            roleIds.forEach(x->{
                TNeusoftSysUserRoleEntity userRole  =new TNeusoftSysUserRoleEntity();
                userRole.setUserId(userEntity.getUserId());
                userRole.setRoleId(Long.valueOf(x));
                neusoftSysUserRoleService.insert(userRole);
            });
        }
    }
}